package com.example.sunmiv1.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.DeliveryTypeBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DeliveryTypeAdapter extends BaseAdapter {

    private List<DeliveryTypeBean> list;
    private ViewHolder viewHolder;

    public DeliveryTypeAdapter(List<DeliveryTypeBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.delivery_type_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        DeliveryTypeBean bean = list.get(position);
        viewHolder.deliveryTypeImg.setImageResource(bean.getImage());
        viewHolder.deliveryTypeName.setText(bean.getName());
        if (bean.isCheck()) {
            viewHolder.deliveryTypeCheck.setImageResource(R.mipmap.checked);
        } else {
            viewHolder.deliveryTypeCheck.setImageResource(R.mipmap.unchecked);
        }
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.deliveryType_img)
        ImageView deliveryTypeImg;
        @BindView(R.id.deliveryType_name)
        TextView deliveryTypeName;
        @BindView(R.id.deliveryType_check)
        ImageView deliveryTypeCheck;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
